package ai.fabu.dataplatform.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class JSONUtil {
    public static Map<String, Object> convertJsonString2Map(String jsonString){
        Map<String, Object> result;
        try{
            result = JSONObject.parseObject(jsonString);
        } catch (JSONException e){
            log.error("convertJsonString2Map: convert error, jsonString: {}", jsonString);
            throw e;
        }
        return result;
    }

    public static List<Object> convertJSonString2List(String jsonString){
        List<Object> result;
        try{
            result = JSONArray.parseArray(jsonString);
        } catch (JSONException e){
            log.error("convertJSonString2List: convert error, jsonString: {}", jsonString);
            throw e;
        }
        return result;
    }

    public Object convertJSONObject2Object(String jsonString){
        return null;
    }

    public static byte[] convertList2Bytes(List<Object> list){
        String string = list.toString();
        string = string.replace('=', ':');
        byte[] result = string.getBytes();
        return result;
    }
}
